package com.xj.opcua.client.manager;

import java.util.concurrent.CompletableFuture;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.xj.opcua.client.Client;
import com.xj.opcua.client.ClientRunner;

public class OpcUaClientManager extends Client{
	private final static Logger logger = LoggerFactory.getLogger(OpcUaClientManager.class);
	
	public OpcUaClientManager() {
	}

	public OpcUaClientManager(String url) {
		super(url);
	}


	public JSONObject bootstrap(String url){
		JSONObject jsonObject = new JSONObject();
		if(Client.map.containsKey(url)) {
			jsonObject.put("code", 201);
			jsonObject.put("msg", "this url is already connected!");
		}else {
			Client client = new OpcUaClientManager(url);
	        try {
				new ClientRunner(client).run();
				jsonObject.put("code", 200);
				jsonObject.put("msg", "this url connect success!");
				logger.info(url+" is connect success!");
			} catch (Exception e) {
				logger.error("Error running connect: {}", e.getMessage(), e);
				jsonObject.put("code", 500);
				jsonObject.put("msg", "this url connect failed!");
			}
		}
		return 	jsonObject;
    }
	
	/**
	 * 客户端连接后的处理
	 */
	@Override
	public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future,String url) throws Exception{
		
	}

}
